from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin, CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, \
    DestroyModelMixin
from .response import APIResponse
from utils.children_list import get_children_list
from django.contrib.auth.models import AnonymousUser


# 获取登录用户
def _get_login_admin(request):
    # 如果用户存在且不是匿名用户
    if request.user and not isinstance(request.user, AnonymousUser):
        return request.user


# 获取列表 可分页
class CustomListModelMixin(ListModelMixin):
    def list(self, request, *args, **kwargs):
        res = super().list(request, *args, **kwargs)
        login_admin = _get_login_admin(request)
        return APIResponse(data=res.data, new_token_admin=login_admin)


# 获取树状列表
class CustomListTreeModelMixin(ListModelMixin):
    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        serializer = self.get_serializer(queryset, many=True)
        res = get_children_list(serializer.data, s_tree=True)
        login_admin = _get_login_admin(request)
        return APIResponse(data=res, new_token_admin=login_admin)


# 添加
class CustomCreateModelMixin(CreateModelMixin):
    def create(self, request, *args, **kwargs):
        res = super().create(request, *args, **kwargs)
        login_admin = _get_login_admin(request)
        return APIResponse(data=res.data, code=201, new_token_admin=login_admin)


# 获取单个
class CustomRetrieveModelMixin(RetrieveModelMixin):
    def retrieve(self, request, *args, **kwargs):
        res = super().retrieve(request, *args, **kwargs)
        login_admin = _get_login_admin(request)
        return APIResponse(data=res.data, new_token_admin=login_admin)


# 更新
class CustomUpdateModelMixin(UpdateModelMixin):
    def update(self, request, *args, **kwargs):
        res = super().update(request, *args, **kwargs)
        login_admin = _get_login_admin(request)
        return APIResponse(code=202, data=res.data, new_token_admin=login_admin)


# 删除
class CustomDestroyModelMixin(DestroyModelMixin):
    def destroy(self, request, *args, **kwargs):
        instance = self.get_object()
        instance.is_delete = True
        instance.save()
        login_admin = _get_login_admin(request)
        return APIResponse(code=204, data=instance.id, new_token_admin=login_admin)


class CustomModelViewSet(CustomListModelMixin,
                         CustomCreateModelMixin,
                         CustomRetrieveModelMixin,
                         CustomUpdateModelMixin,
                         CustomDestroyModelMixin,
                         GenericViewSet):
    pass


class CustomTreeModelViewSet(CustomListTreeModelMixin,
                             CustomCreateModelMixin,
                             CustomRetrieveModelMixin,
                             CustomUpdateModelMixin,
                             CustomDestroyModelMixin,
                             GenericViewSet):
    pass


class CustomReadOnlyModelViewSet(CustomListModelMixin, CustomRetrieveModelMixin, GenericViewSet):
    pass


class CustomTreeReadOnlyModelViewSet(CustomListTreeModelMixin, CustomRetrieveModelMixin, GenericViewSet):
    pass
